
//渲染页面------------------------------------------------------------------------------------
let data = {
    pagenum: 1,
    pagesize: 3,
};
function getdata() {
    $.ajax({
        url: '/admin/users',
        data: data,
        success: function (res) {
            // console.log(res)
            showpage(res.total);
            let html = template('tpl-list', res)
            $('tbody').html(html)
        },

    })
};
getdata()
//分页-------------------------------------------------------------------------------------
var laypage = layui.laypage;

//执行一个laypage实例
function showpage(a) {
    laypage.render({
        elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
        , count: a,//数据总数，从服务端得到
        limit: data.pagesize,
        limits: [2, 3, 5, 10],
        curr: data.pagenum,
        layout: ['prev', 'page', 'next', 'count', 'limit', 'skip'],
        jump: function (obj, first) {
            if (first === undefined) {
                data.pagenum = obj.curr;
                data.pagesize = obj.limit;
                getdata();
            }
        }
    });
};
//删除功能------------------------------------------------------------------------
$('body').on('click', '.del', function () {
    let id = $(this).data('id');
    layer.confirm('确定删除？', function (index) {
        $.ajax({
            type: 'DELETE',
            url: '/admin/users/' + id,
            success: function (res) {
                console.log(res)
                layer.msg(res.message);
                if (res.status === 0) {
                    getdata();
                }
            },


        })
        layer.close(index);
    })
});
//重置密码功能--------------------------------------------------------------
var w;
$('body').on('click', 'a:contains("重置密码")', function () {
    let data = $(this).data();
    w = layer.open({
        type: 1,
        title: '添加类别'
        , content: $('#wp').html(),
        area: ['500px', '300px'],
        success: function (layero, index) {

            $('input[name=id]').val(data.id);
        }
    });
});
//功能
$('body').on('submit', '.wp', function (e) {
    e.preventDefault();
    // if ($('.pas').val() !== $('.newpas').val()) {
    //     layer.msg('两次密码请输入一样的');
    //     // alert('两次密码请输入一样的')
    //     return;
    // }
    let data = $(this).serializeArray();
    //console.log(data)
    $.ajax({
        url: '/admin/users/' + data[1].value,
        type: 'PUT',
        data: { password: data[0].value },
        success: function (res) {
            console.log(res)
            layer.msg(res.message);
            if (res.status === 0) {
                layer.close(w);
            }
        },

    })
});
//编辑-----------------------------------------------------------------------------------------------------------------------------------
var y;
$('body').on('click', 'a:contains("编辑")', function () {
    let data = $(this).data();
    //console.log(data)
    y = layer.open({
        type: 1,
        title: '编辑用户'
        , content: $('#bianji').html(),
        area: ['500px', '300px'],
        success: function (layero, index) {
            $('input[name=username]').val(data.username);
            $('input[name=id]').val(data.id);
            $('input[name=nickname]').val(data.nickname);
            $('input[name=email]').val(data.email);
        }
    });
});
//功能
$('body').on('submit', '.bianji', function (e) {
    e.preventDefault();
    let data = $(this).serialize();
    //console.log(data)
    $.ajax({
        url: '/admin/users',
        type: 'PUT',
        data: data,
        success: function (res) {
            console.log(res)
            layer.msg(res.message)
            if (res.status === 0) {
                getdata();
                layer.close(y)
            }
        },

    })
})